package org.sgs.toolbox.sandbox;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.sgs.toolbox.sql.GenericRecord;
import org.sgs.toolbox.sql.GenericRecordList;
import org.sgs.toolbox.sql.SqlUtils;



public class Sandbox {
	
	
	public static void runExampleOne(){
		
		Map<String, String> cabObjectNameToTableMap = new HashMap<String, String>();
		
		// org.kuali.kfs.module.cab.businessobject.GeneralLedgerEntryAsset
		cabObjectNameToTableMap.put("GeneralLedgerEntryAsset", "CB_GL_ENTRY_T");
		
		//org.kuali.kfs.module.cab.businessobject.GeneralLedgerEntry
		cabObjectNameToTableMap.put("GeneralLedgerEntry", "CB_GL_ENTRY_AST_T");
		
		//org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableDocument
		cabObjectNameToTableMap.put("PurchasingAccountsPayableDocument", "CB_PUR_DOC_T");
		
		//org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableLineAssetAccount
		cabObjectNameToTableMap.put("PurchasingAccountsPayableLineAssetAccount",  "CB_PUR_LN_AST_ACCT_T");
		
		//org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableItemAsset
		cabObjectNameToTableMap.put("PurchasingAccountsPayableItemAsset",  "CB_PUR_ITM_AST_T");
		
		//org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableActionHistory
		cabObjectNameToTableMap.put("PurchasingAccountsPayableActionHistory", "CB_PUR_ACTN_HIST_T");
		
		//org.kuali.kfs.module.cab.businessobject.Pretag
		cabObjectNameToTableMap.put("Pretag", "CB_PRTG_T");
		
		//org.kuali.kfs.module.cab.businessobject.PretagDetail
		cabObjectNameToTableMap.put("PretagDetail", "CB_PRTG_DTL_T");
		
		//org.kuali.kfs.module.cab.businessobject.AssetTransactionType
		cabObjectNameToTableMap.put("AssetTransactionType", "CB_AST_TRN_TYP_T");
	    
		// Example for doing a count(*) on a table
	    String queryFormat = "select count(*) from %s";
	    for(Entry<String, String> entry : cabObjectNameToTableMap.entrySet()){
	    	String objectName = entry.getKey();
	    	String tableName = entry.getValue();
	    	String queryString = String.format(queryFormat, tableName);
	    	int count = SqlUtils.queryForInt(queryString);
		    System.out.printf("%42s: %d\n", objectName, count);
	    }
	}
	
	public static void runExampleTwo(GenericRecordList resultList){
		// Example of select * from table
	    for(int i = 0; i < resultList.getColumnCount(); i++){
	    	System.out.printf("%s ", resultList.getColumnName(i));
	    }
	    System.out.printf("\n");
	    while(resultList.hasNext()){
	    	GenericRecord record = resultList.next();
	    	System.out.printf("%s\n", record.toString());
	    }
	    
	}
	
	public static void runExampleThree(GenericRecordList resultList){
	    // Example of csv output
	    System.out.printf("%s", resultList.asCsv());
	}
	
	public static void main(String[] sgs) {
		
		/*
		GenericRecordList resultList = SqlUtils.queryForList("select * from CB_GL_ENTRY_T");
		runExampleOne();
		runExampleTwo(resultList);
		runExampleThree(resultList);
		*/
		
		
		StringBuilder sb = new StringBuilder();
		for(String arg : sgs){
			sb.append(arg);
			sb.append(" ");
		}
		
		System.out.printf("'%s'\n", sb.toString());
	}
	
}
